#include "app.hpp"

namespace midian
{
    App::App() : running(false), ticks(0), lasttick(0), delta(0), frametime(0)
    {

    }

    //Start SDL, set the video mode and setup opengl
    int Game::Init(std::string cmpname)
    {
        //Initialize SDL -- Windows doesn't need SDL_INIT_EVENTTHREAD and SDL_Init will return unsupported OS
#ifdef __WIN32__
        if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) == -1)
#else
        if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) == -1)
#endif
        {
            std::cout << "Error starting SDL: " << SDL_GetError() << std::endl;
            return(0);
        }

        //Initialize Fastevents
        if (FE_Init() == -1)
        {
            std::cout << "Error starting Fast Events: " << FE_GetError() << std::endl;
            return(0);
        }

        //Guichan needs this
        SDL_EnableUNICODE(1);
        SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

        //Try to open the window and set up the opengl state
        if(!UpdateVideo()) { return(0); }

        return(1);
    }

    //Set the video mode and load resources
    int Game::UpdateVideo()
    {
        //Try to enable FSAA
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
        SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

        //SDL Window
        if(conf.screenmode.fs)
        {
            screen = SDL_SetVideoMode(conf.screenmode.screenx, conf.screenmode.screeny, conf.screenmode.bpp, SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF|SDL_FULLSCREEN);
        }
        else
        {
            screen = SDL_SetVideoMode(conf.screenmode.screenx, conf.screenmode.screeny, conf.screenmode.bpp, SDL_HWSURFACE|SDL_OPENGL|SDL_DOUBLEBUF);
        }

        if(!screen)
        {
            std::cout << "Error setting video mode" << std::endl; return(0);
        }

        SDL_WM_SetCaption("Midian","Midian");

        return(1);

    }

    //Loop loop loop, all day long
    void Game::Run()
    {
        running = true;
        while(running)
        {
            //The CPU would appreciate it if we took a break from time to time
            SDL_Delay(5);
            ticks = SDL_GetTicks();
            frametime = delta = ticks - lasttick;
            lasttick = ticks;

            //Process events
            while(FE_PollEvent(&ev))
            {
                //Let guichan see what's going on
                //guicore.input->pushInput(ev);

                //Pass the event to the handler
                //input->ProcEvent(&ev);
            }

            //Update gamestate and then draw the scene
            logic();
            display();
        }
    }

    void Game::display()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glLoadIdentity();

        //Draw the scene
        //glEnable(GL_LIGHTING);
        //scene.Render();
        //glDisable(GL_LIGHTING);

        //Draw any visible guichan elements
        //guicore.gui->draw();

        //Update the screen
        SDL_GL_SwapBuffers();
    }

}
